﻿using System;
using System.Threading.Tasks;
using System.Web;
using JT.WEB.Areas.Identity.Data;
using JT.WEB.Models;
using JT.WEB.Service;
using JTWEB3.Models;
using JTWEB3.Views.News;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using NewsPublish.Model.Request;
using NewsPublish.Model.Response;

namespace JTWEB3.Controllers
{
    public class NewsController : Controller
    {
        private readonly NewsService _newsService;
        private readonly BannerService _bannerService;
        private readonly CommentService _commentService;
        private readonly UserManager<JTWEB3User> _jtweb3User;

        public NewsController(NewsService newsService, BannerService bannerService, CommentService commentService, UserManager<JTWEB3User> jTWEB3User)
        {
            _newsService = newsService;
            _bannerService = bannerService;
            _commentService = commentService;
            _jtweb3User = jTWEB3User;
        }
        // GET: News
        public ActionResult Index()
        {
            ViewData["Title"] = "新闻首页";
            IndexModel vm = new IndexModel { };
            vm.NewsClassifyList = _newsService.GetNewsClassifyList();
            vm.NewsList = _newsService.GetNewsList(c => c.Id > 0, 5);
            vm.HotNewsList = _newsService.GetNewsList(c => c.Remark != null, 5);
            if (vm is null)
            {
                return View();
            }
            return View(vm);
        }

        // GET: News/Details/5
        public async Task<IActionResult> Details(int id)
        {
            IndexModel vm = new IndexModel { };
            vm.News = _newsService.GetOneNews(id);
            vm.HotNewsList = _newsService.GetNewsList(c => c.Remark != null, 5);
            vm.CommentList = _commentService.GetCommentList(c => c.NewsId == id);
            JTWEB3User res = await _jtweb3User.GetUserAsync(HttpContext.User).ConfigureAwait(false);
            HttpContext.Request.RouteValues.TryGetValue("controller", out object a);
            ViewData["controller"] = a.ToString();
            return View(vm);
        }

        // GET: News/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: News/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: News/Edit/5
        public ActionResult Edit()
        {
            return View();
        }

        // POST: News/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: News/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: News/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
        //新闻搜索
        public IActionResult Search(string searchString)
        {
            if (string.IsNullOrEmpty(searchString))
            {
                return RedirectToAction(nameof(Index));
            }
            SearchModel vm = new SearchModel { NewsList = _newsService.GetNewsList(where: c => c.Title.Contains(searchString), 5) };
            if (vm.NewsList.code == 0)
            {
                vm.searchResult = $"很抱歉，没有找到与 “{searchString}” 相关的网页。";
            }
            return View(vm);
        }
        [HttpPost]
        public JsonResult AddComment(AddComment addComment)
        {
            if (addComment is null)
            {
                ModelState.AddModelError(string.Empty, "评论不能为空");
                return Json(new ResponseModel { code = 0, result = "评论不能为空" });
            }
            return Json(_commentService.AddComment(addComment));
        }
    }
}